import type {
  AddReq,
  CreateCrudOptionsProps,
  CreateCrudOptionsRet,
  DelReq,
  EditReq,
  UserPageQuery,
} from '@fast-crud/fast-crud';

import { dict, utils } from '@fast-crud/fast-crud';

import * as api from './api';

export default function crud(
  props: CreateCrudOptionsProps,
): CreateCrudOptionsRet {
  utils.logger.debug('crud props', props);
  return {
    crudOptions: {
      request: {
        addRequest: async ({ form }: AddReq) => await api.AddObj(form),
        delRequest: async ({ row }: DelReq) => await api.DelObj(row.id),
        editRequest: async ({ form }: EditReq) => await api.UpdateObj(form),
        pageRequest: async (query: UserPageQuery) => await api.GetPage(query),
      },
      columns: {
        id: {
          title: 'ID',
          type: 'text',
          form: { show: false },
          column: { show: false },
        },
        code: {
          title: '编码',
          type: 'text',
          column: { width: 100 },
        },
        name: {
          title: '名称',
          type: 'text',
          column: { width: 150 },
          search: { show: true },
          form: {
            rules: [
              { required: true, message: '岗位名称不能为空' },
              { min: 1, max: 30, message: '长度在 1 到 30 个字符' },
            ],
          },
        },
        orgId: {
          title: '组织',
          search: { show: true, order: 0 },
          column: { width: 200, component: { color: 'auto' } },
          type: 'dict-tree',
          dict: dict({
            isTree: true,
            url: '/iam/system/org/trees',
            value: 'id',
            label: 'name',
          }),
          form: {
            col: { span: 24 },
            rules: [{ required: true, message: '组织名称不能为空' }],
          },
        },
        remark: {
          title: '描述',
          search: { show: false },
          type: ['textarea'],
          form: {
            col: { span: 24 },
          },
          column: { width: 320, ellipsis: true },
        },
        sequence: {
          title: '排序',
          column: { width: 80, align: 'center' },
          type: 'number',
          addForm: {
            value: 0,
            component: { min: 0, max: 100 },
          },
          editForm: { component: { min: 0, max: 100 } },
        },
        status: {
          title: '状态',
          type: 'dict-radio',
          column: { width: 100, align: 'center' },
          search: { show: true },
          addForm: {
            value: 1,
          },
          dict: dict({
            data: [
              { value: true, label: '启用', color: 'success' },
              { value: false, label: '禁用', color: 'error' },
            ],
          }),
        },
        createdTime: {
          title: '创建时间',
          type: ['datetime', 'wp-readonly-time'],
        },
      },
    },
  };
}
